package com.fjycnet.file;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.orangelife.R;

public class MyFileActivity extends AppCompatActivity {

    private final int REQUEST_CODE_ALBUM = 1;
    private ImageView ivPhoto;
    private TextView tvPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_file);
        ivPhoto = findViewById(R.id.ivPhoto);
        tvPath = findViewById(R.id.tvPath);
        findViewById(R.id.btnOpenAlbum).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openAlbum();
            }
        });
    }

    /**
     * 打开相机.
     * 这两个是常用功能,所以放一起.
     */
    private void openCamera() {

    }

    /**
     * 从相册选择原生的照片（不裁切）
     */
    private void openAlbum() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);//Pick an item fromthe data
        intent.setType("image/*");//从所有图片中进行选择
        startActivityForResult(intent, REQUEST_CODE_ALBUM);
    }

    private void handlePhoto(Intent data) {
        Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        if (selectedImage != null) {
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
            if (cursor != null) {
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);  //获取照片路径
                Log.e(MyFileActivity.class.getSimpleName(), "path=" + String.valueOf(picturePath));
                cursor.close();
                tvPath.setText(String.valueOf(picturePath));
                Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
                ivPhoto.setImageBitmap(bitmap);
            } else {
                Toast.makeText(this, "cursor is null", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "selectedImage is null", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_ALBUM:
                if (resultCode == RESULT_OK) {//从相册选择照片不裁切
                    handlePhoto(data);
                }
                break;
        }
    }
}
